<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/7/28
 * Time: 17:00
 */

namespace app\reception\controller;


use app\common\model\Auditor;
use app\common\model\AuditorEvent;
use app\common\model\QuickApp;
use app\common\model\UserBlack;
use app\common\model\UserWhite;
use think\Controller;
use think\facade\Request;

class Common extends Controller
{
    public $package;
    public $apiVersion;
    public $quickAppId;
    public $userToken;
    public $uid;
    public $user;
    public $isWhite = 0;//白名单
    public $isBlack = 0;//黑名单，1：拉黑 2：绝对拉黑

    public function initialize()
    {
//        for ($i = 0; $i <= 99; $i++) {
//            echo "ALTER TABLE `quick_app`.`quick_user_{$i}` ADD INDEX `phone` (`phone`);" . "<br>";
//        }
//        die;
        $this->package = request()->header('x-quick-package');
        $this->apiVersion = request()->header('x-api-version');
        $this->quickAppId = QuickApp::where('package_name', $this->package)->value('id');
        if (!$this->quickAppId) {
            errJson('quick app is null')->send();
            die;
        }
        $this->userToken = request()->header('x-user-token');
        if ($this->userToken) {
            $userModel = app('user');
            $this->user = $userModel->getByToken($this->userToken);
            if ($this->user) {
                if ($this->quickAppId != $this->user->quick_id) {
                    errJson('用户信息和快应用信息比对失败，非法传参')->send();
                    die;
                }
                $this->uid = request()->uid = $this->user->id;
                if (UserWhite::get($this->uid)) {
                    $this->isWhite = 1;
                } else {
                    $this->isBlack = UserBlack::where("userId", $this->user->userId)->value('status') ?? 0;
                    /**查看有无审核事件，记录访问用户*/
                    $where[] = ['api_version', '=', $this->apiVersion];
                    $where[] = ['quick_id', '=', $this->quickAppId];
                    $where[] = ['status', '=', 1];
                    $where[] = ['provider', 'in', ['', $this->user->provider]];
                    $eventId = AuditorEvent::where($where)->order('id desc')->value('id');
                    if ($eventId) {
                        $auditor = Auditor::where('event_id', $eventId)->where('uid', $this->uid)->find();
                        if (!$auditor) {
                            Auditor::insert([
                                'userId' => $this->user->userId,
                                'event_id' => $eventId,
                                'api_version' => $this->apiVersion,
                                'quick_id' => $this->quickAppId,
                                'status' => 1,
                                'uid' => $this->uid,
                                'ip' => request()->ip()
                            ]);
                        }
                    }
                }
                if ($this->isBlack == 2) {
                    errJson('abnormal user behavior')->send();
                    die;
                }
            }
        }
        parent::initialize(); // TODO: Change the autogenerated stub
    }
}